package com.baorun.test.web.controlller;

import com.baorun.entity.Book;
import com.baorun.entity.Result;
import com.baorun.test.web.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author: wang
 * @date: 2021/9/1 10:05
 * @description:
 */
@RestController
@Slf4j
public class BookController {

    @Value("${name}")
    private String name;

    @Resource
    private BookService bookService;

    @GetMapping("/book")
    public Result queryBooks(){
        log.info(name);
        List<Book> books = bookService.queryBooks();
        Result<List<Book>> result = new Result<>();
        if ( books != null ){
            result.setCode(200);
            result.setMsg("查询成功"+name);
            result.setData(books);
        }else{
            result.setCode(4444);
            result.setMsg("查询失败"+name);
            result.setData(books);
        }
        return result;
    }

    @GetMapping("/timeOut")
    public Result timeOut(){
        try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
        List<Book> books = bookService.queryBooks();
        Result<List<Book>> result = new Result<>();
        if ( books != null ){
            result.setCode(200);
            result.setMsg("查询成功"+name);
            result.setData(books);
        }else{
            result.setCode(4444);
            result.setMsg("查询失败"+name);
            result.setData(books);
        }
        return result;
    }


}
